<template>
  <div class="order-container">
    <el-card class="order-card">
      <template #header>
        <div class="card-header">
          <span>订单列表</span>
          <div class="header-actions">
            <el-input
              v-model="searchForm.keyword"
              placeholder="请输入订单号或用户手机号"
              style="width: 300px; margin-right: 10px"
              clearable
              @clear="searchOrders"
              @keyup.enter="searchOrders"
            >
              <template #append>
                <el-button icon="Search" @click="searchOrders" />
              </template>
            </el-input>
            <el-button type="primary" icon="Refresh" @click="refreshOrders">刷新</el-button>
          </div>
        </div>
      </template>

      <el-table
        :data="orderList"
        style="width: 100%"
        v-loading="loading"
      >
        <el-table-column prop="id" label="订单ID" width="80" />
        <el-table-column prop="orderNo" label="订单号" width="180" />
        <el-table-column prop="userName" label="用户名" width="120" />
        <el-table-column prop="merchantName" label="商户名称" width="150" />
        <el-table-column prop="amount" label="订单金额" width="100">
          <template #default="scope">
            ¥{{ scope.row.amount }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="订单状态" width="120">
          <template #default="scope">
            <el-tag v-if="scope.row.status === 'pending'" type="info">待支付</el-tag>
            <el-tag v-else-if="scope.row.status === 'paid'" type="primary">已支付</el-tag>
            <el-tag v-else-if="scope.row.status === 'shipped'" type="warning">已发货</el-tag>
            <el-tag v-else-if="scope.row.status === 'completed'" type="success">已完成</el-tag>
            <el-tag v-else-if="scope.row.status === 'cancelled'" type="danger">已取消</el-tag>
            <el-tag v-else>{{ scope.row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="下单时间" width="180" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" @click="handleViewDetail(scope.row)">详情</el-button>
            <el-button
              v-if="scope.row.status === 'pending'"
              size="small"
              type="primary"
              @click="handleProcessOrder(scope.row)"
            >
              处理订单
            </el-button>
            <el-button
              v-if="scope.row.status === 'paid'"
              size="small"
              type="warning"
              @click="handleShipOrder(scope.row)"
            >
              发货
            </el-button>
            <el-button
              v-if="['pending', 'paid', 'shipped'].includes(scope.row.status)"
              size="small"
              type="danger"
              @click="handleCancelOrder(scope.row)"
            >
              取消
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 订单详情对话框 -->
    <el-dialog
      v-model="detailDialogVisible"
      title="订单详情"
      width="600px"
    >
      <el-descriptions :column="1" border>
        <el-descriptions-item label="订单号">{{ currentOrder.orderNo }}</el-descriptions-item>
        <el-descriptions-item label="用户">{{ currentOrder.userName }}</el-descriptions-item>
        <el-descriptions-item label="商户">{{ currentOrder.merchantName }}</el-descriptions-item>
        <el-descriptions-item label="订单金额">¥{{ currentOrder.amount }}</el-descriptions-item>
        <el-descriptions-item label="订单状态">
          <el-tag v-if="currentOrder.status === 'pending'" type="info">待支付</el-tag>
          <el-tag v-else-if="currentOrder.status === 'paid'" type="primary">已支付</el-tag>
          <el-tag v-else-if="currentOrder.status === 'shipped'" type="warning">已发货</el-tag>
          <el-tag v-else-if="currentOrder.status === 'completed'" type="success">已完成</el-tag>
          <el-tag v-else-if="currentOrder.status === 'cancelled'" type="danger">已取消</el-tag>
          <el-tag v-else>{{ currentOrder.status }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="下单时间">{{ currentOrder.createTime }}</el-descriptions-item>
        <el-descriptions-item label="收货地址">{{ currentOrder.address }}</el-descriptions-item>
      </el-descriptions>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="detailDialogVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索表单
const searchForm = reactive({
  keyword: ''
})

// 订单列表
const orderList = ref([
  {
    id: 1,
    orderNo: 'ORD202301010001',
    userName: '张三',
    merchantName: '肯德基',
    amount: 89.50,
    status: 'pending',
    createTime: '2023-01-01 12:00:00',
    address: '北京市朝阳区xxx街道xxx号'
  },
  {
    id: 2,
    orderNo: 'ORD202301010002',
    userName: '李四',
    merchantName: '麦当劳',
    amount: 56.00,
    status: 'paid',
    createTime: '2023-01-01 13:30:00',
    address: '北京市海淀区xxx街道xxx号'
  },
  {
    id: 3,
    orderNo: 'ORD202301010003',
    userName: '王五',
    merchantName: '必胜客',
    amount: 128.00,
    status: 'shipped',
    createTime: '2023-01-01 15:45:00',
    address: '北京市西城区xxx街道xxx号'
  },
  {
    id: 4,
    orderNo: 'ORD202301010004',
    userName: '赵六',
    merchantName: '汉堡王',
    amount: 68.50,
    status: 'completed',
    createTime: '2023-01-01 18:20:00',
    address: '北京市东城区xxx街道xxx号'
  },
  {
    id: 5,
    orderNo: 'ORD202301010005',
    userName: '钱七',
    merchantName: '星巴克',
    amount: 42.00,
    status: 'cancelled',
    createTime: '2023-01-02 10:15:00',
    address: '北京市丰台区xxx街道xxx号'
  }
])

// 分页
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 5
})

// 加载状态
const loading = ref(false)

// 当前选中的订单
const currentOrder = ref({})

// 详情对话框
const detailDialogVisible = ref(false)

// 搜索订单
const searchOrders = () => {
  ElMessage.info('搜索功能已触发')
  console.log('搜索关键字:', searchForm.keyword)
}

// 刷新订单列表
const refreshOrders = () => {
  ElMessage.success('订单列表已刷新')
}

// 查看订单详情
const handleViewDetail = (row) => {
  currentOrder.value = row
  detailDialogVisible.value = true
}

// 处理订单
const handleProcessOrder = (row) => {
  ElMessageBox.confirm(`确定要处理订单"${row.orderNo}"吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 更新订单状态
    row.status = 'paid'
    ElMessage.success('订单处理成功')
  }).catch(() => {
    // 取消操作
  })
}

// 发货
const handleShipOrder = (row) => {
  ElMessageBox.confirm(`确定要为订单"${row.orderNo}"发货吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 更新订单状态
    row.status = 'shipped'
    ElMessage.success('订单发货成功')
  }).catch(() => {
    // 取消操作
  })
}

// 取消订单
const handleCancelOrder = (row) => {
  ElMessageBox.confirm(`确定要取消订单"${row.orderNo}"吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 更新订单状态
    row.status = 'cancelled'
    ElMessage.success('订单取消成功')
  }).catch(() => {
    // 取消操作
  })
}

// 分页相关方法
const handleSizeChange = (val) => {
  pagination.pageSize = val
  ElMessage.info(`每页显示 ${val} 条`)
}

const handleCurrentChange = (val) => {
  pagination.currentPage = val
  ElMessage.info(`当前页: ${val}`)
}

// 页面加载时获取数据
onMounted(() => {
  ElMessage.success('订单列表页面加载完成')
})
</script>

<style scoped>
.order-container {
  padding: 20px;
  background-color: #f0f2f5;
  height: 100%;
  box-sizing: border-box;
}

.order-card {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-actions {
  display: flex;
  align-items: center;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>